package com.coderg.gmall.order.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description
 * @Author CoderG
 * @Date 2025/6/13 18:26
 **/
@Configuration
public class RabbitConfig {
    /*
     * 延迟队列
     * 定义正常交换机
     * 定义死信队列
     * 定义死信交换机
     * 定义正常队列
     *  */

    // 正常业务交换机和队列
    public static final String BUSINESS_EXCHANGE = "business.exchange";
    public static final String BUSINESS_QUEUE = "business.queue";
    public static final String BUSINESS_ROUTING_KEY = "business.key";

    // 死信交换机和队列
    public static final String DLX_EXCHANGE = "dlx.exchange";
    public static final String DLX_QUEUE = "dlx.queue";
    public static final String DLX_ROUTING_KEY = "dlx.key";

    // 1. 定义业务交换机
    @Bean
    public DirectExchange businessExchange() {
        return new DirectExchange(BUSINESS_EXCHANGE);
    }


    // 2. 定义死信队列
    @Bean
    public Queue dlxQueue() {
        return QueueBuilder.durable(DLX_QUEUE)
                // 设置死信交换机
                .withArgument("x-dead-letter-exchange", DLX_EXCHANGE)
                // 设置死信路由键
                .withArgument("x-dead-letter-routing-key", DLX_ROUTING_KEY)
                // 可选：设置消息TTL(毫秒)
                // .withArgument("x-message-ttl", 60000 * 30)
                .withArgument("x-message-ttl", 10000)
                // 可选：队列最大长度
                .withArgument("x-max-length", 1000)
                .build();

    }

    // 3. 定义死信交换机
    @Bean
    public DirectExchange dlxExchange() {
        return new DirectExchange(DLX_EXCHANGE);
    }


    // 4. 定义业务队列
    @Bean
    public Queue businessQueue() {
        return QueueBuilder.durable(BUSINESS_QUEUE).build();


    }

    // 5. 业务交换机--死信队列
    @Bean
    public Binding businessExchangeBindDlxQueue(@Qualifier("dlxQueue") Queue queue,
                                                @Qualifier("businessExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(BUSINESS_ROUTING_KEY).noargs();
    }

    // 6 死信交换机--业务队列
    @Bean
    public Binding dlxExchangeBindBusinessQueue(@Qualifier("businessQueue") Queue queue,
                                                @Qualifier("dlxExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(DLX_ROUTING_KEY).noargs();
    }

    //定义重试队列
    public static final String RETRY_QUEUE = "retry.queue";
    //定义重试key
    public static final String RETRY_ROUTING_KEY = "retry.queue";

    @Bean
    public Queue retryQueue() {
        return QueueBuilder.durable(RETRY_QUEUE).build();
    }

    //绑定重试队列和死信交换机
    @Bean
    public Binding dlxExchangeBindRetryQueue(@Qualifier("retryQueue") Queue queue,
                                             @Qualifier("dlxExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(RETRY_ROUTING_KEY).noargs();
    }
}
